﻿// Extensions Format    
String.prototype.format = function () {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        formatted = formatted.replace(RegExp('\\{' + i + '\\}', 'g'), arguments[i]);
    }
    return formatted;
};
String.prototype.replaceAll = function (strTarget, strSubString) {
    var strText = this;
    if (strText.length == 0) return '';
    var intIndexOfMatch = strText.indexOf(strTarget);
    while (intIndexOfMatch != -1) {
        strText = strText.replace(strTarget, strSubString);
        intIndexOfMatch = strText.indexOf(strTarget);
    }
    return strText;
};

// User
var globalUsers;
function bindGlobalUsers() {
    if (globalUsers == null) {
        $.getJSON('/admin/api/user/get').done(function (data) {
            globalUsers = data;
        });
    }
}
function buildParamUsers() {
    var param = '';
    if (globalUsers != null) {
        for (var i = 0; i < globalUsers.length; i++) {
            var id = globalUsers[i].UserID;
            if (!$('#' + id + '_chkUser').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function userFomatter(cellvalue, options, rowObject) {
    if (globalUsers == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalUsers, function (index) {

        if (globalUsers[index].UserID == cellvalue) {
            retVal = globalUsers[index].FullName;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == 0 ? '' : cellvalue;
}

// Level
var globalLevels;
function bindGlobalLevels() {
    if (!globalLevels) {
        $.getJSON('/admin/api/level/get').done(function (data) {
            globalLevels = data;
        });
    }
}
function buildParamLevels() {
    var param = '';
    if (globalLevels != null) {
        for (var i = 0; i < globalLevels.length; i++) {
            var id = globalLevels[i].LevelId;
            if (!$('#' + id + '_chkLevel').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function levelFomatter(cellvalue, options, rowObject) {
    if (globalLevels == null) {
        return cellvalue === undefined || cellvalue == null ? '' : cellvalue;
    }
    if (cellvalue == 0) cellvalue = 1;
    var retVal;
    $.each(globalLevels, function (index) {

        if (globalLevels[index].LevelId == cellvalue) {
            retVal = globalLevels[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null ? '' : cellvalue;
}
function levelUnformat(cellvalue, options) {
    //console.log(cellvalue);
    $.each(globalLevels, function (index) {
        if (globalLevels[index].Name == cellvalue) {
            return globalLevels[index].LevelId;
        }
    });
    return cellvalue;
}
function jqGridLevelDropVal(elem, operation, value) {
    if (operation === 'get') {
        return $(elem).val();
    } else if (operation === 'set') {
        $(elem).val(value);
    }
};
function jqGridLevelDropEl(value, options) {
    var retVal = $(document.createElement('select'));
    $(retVal).addClass("form-control");

    $.each(globalLevels, function (index) {
        if (globalLevels[index].LevelId == value) {
            $(retVal).append("<option selected value='" + globalLevels[index].LevelId + "'>" + globalLevels[index].Name + "</option>");
        } else {
            $(retVal).append("<option value='" + globalLevels[index].LevelId + "'>" + globalLevels[index].Name + "</option>");
        }
    });
    return retVal;
}

// Major
var globalMajors;
function bindGlobalMajors() {
    if (!globalMajors) {
        $.getJSON('/admin/api/major/get').done(function (data) {
            globalMajors = data;
        });
    }
}
function buildParamMajors() {
    var param = '';
    if (globalMajors != null) {
        for (var i = 0; i < globalMajors.length; i++) {
            var id = globalMajors[i].MajorId;
            if (!$('#' + id + '_chkMajor').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function majorFomatter(cellvalue, options, rowObject) {
    if (globalMajors == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalMajors, function (index) {

        if (globalMajors[index].MajorId == cellvalue) {
            retVal = globalMajors[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// Location
var globalLocations;
function bindGlobalLocations() {
    if (!globalLocations) {
        $.getJSON('/admin/api/location/get').done(function (data) {
            globalLocations = data;
        });
    }
}
function locationFomatter(cellvalue, options, rowObject) {
    if (globalLocations == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalLocations, function (index) {

        if (globalLocations[index].LocationId == cellvalue) {
            retVal = globalLocations[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// Status
var globalStatus;
function bindGlobalStatus() {
    if (!globalStatus) {
        $.getJSON('/admin/api/status/get').done(function (data) {
            globalStatus = data;
        });
    }
}
function buildParamStatus() {
    var param = '';
    if (globalStatus != null) {
        for (var i = 0; i < globalStatus.length; i++) {
            var id = globalStatus[i].StatusId;
            if (!$('#' + id + '_chkStatus').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function statusFomatter(cellvalue, options, rowObject) {
    if (globalStatus == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalStatus, function (index) {

        if (globalStatus[index].StatusId == cellvalue) {
            retVal = globalStatus[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// Collectors
var globalCollectors;
function collectorFomatter(cellvalue, options, rowObject) {
    if (globalCollectors == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalCollectors, function (index) {

        if (globalCollectors[index].CollectorId == cellvalue) {
            retVal = globalCollectors[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// StatusMaps
var globalStatusMaps;
function bindGlobalStatusMaps() {
    if (!globalStatusMaps) {
        $.getJSON('/admin/api/statusMap/get').done(function (data) {
            globalStatusMaps = data;
        });
    }
}
function buildParamStatusMaps() {
    var param = '';
    if (globalStatusMaps != null) {
        for (var i = 0; i < globalStatusMaps.length; i++) {
            var id = globalStatusMaps[i].Id;
            if (!$('#' + id + '_chkStatusMap').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function statusMapFomatter(cellvalue, options, rowObject) {
    if (globalStatusMaps == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalStatusMaps, function (index) {

        if (globalStatusMaps[index].Id == cellvalue) {
            retVal = globalStatusMaps[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}
function jqGridStatusMapDropVal(elem, operation, value) {
    if (operation === 'get') {
        return $(elem).val();
    } else if (operation === 'set') {
        $(elem).val(value);
    }
};
function jqGridStatusMapDropEl(value, options) {
    var retVal = $(document.createElement('select'));
    //retval.innerHTML = "<option value='1'>HN</option>";
    $(retVal).addClass("form-control");

    $.each(globalStatusMaps, function (index) {
        if (globalStatusMaps[index].Id == value) {
            $(retVal).append("<option selected value='" + globalStatusMaps[index].Id + "'>" + globalStatusMaps[index].Name + "</option>");
        } else {
            $(retVal).append("<option value='" + globalStatusMaps[index].Id + "'>" + globalStatusMaps[index].Name + "</option>");
        }
    });
    //});
    return retVal;
}

// StatusCares
var globalStatusCares;
function bindGlobalStatusCares() {
    if (!globalStatusCares) {
        $.getJSON('/admin/api/statusCare/get').done(function (data) {
            globalStatusCares = data;
        });
    }
}
function buildParamStatusCares() {
    var param = '';
    if (globalStatusCares != null) {
        for (var i = 0; i < globalStatusCares.length; i++) {
            var id = globalStatusCares[i].Id;
            if (!$('#' + id + '_chkStatusCare').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function statusCareFomatter(cellvalue, options, rowObject) {
    if (globalStatusCares == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalStatusCares, function (index) {

        if (globalStatusCares[index].Id == cellvalue) {
            retVal = globalStatusCares[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// School
var globalSchools;
function bindGlobalSchools() {
    if (!globalSchools) {
        $.getJSON('/admin/api/school/get').done(function (data) {
            globalSchools = data;
        });
    }
}
function buildParamSchools() {
    var param = '';
    if (globalSchools != null) {
        for (var i = 0; i < globalSchools.length; i++) {
            var id = globalSchools[i].SchoolId;
            if (!$('#' + id + '_chkSchool').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function schoolFomatter(cellvalue, options, rowObject) {
    if (globalSchools == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalSchools, function (index) {

        if (globalSchools[index].SchoolId == cellvalue) {
            retVal = globalSchools[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// Channel
var globalChannels;
function bindGlobalChannels() {
    if (!globalChannels) {
        $.getJSON('/admin/api/channel/get').done(function (data) {
            globalChannels = data;
        });
    }
}
function buildParamChannels() {
    var param = '';
    if (globalChannels != null) {
        for (var i = 0; i < globalChannels.length; i++) {
            var id = globalChannels[i].ChannelId;
            if (!$('#' + id + '_chkChannel').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function channelFomatter(cellvalue, options, rowObject) {
    if (globalChannels == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalChannels, function (index) {

        if (globalChannels[index].ChannelId == cellvalue) {
            retVal = globalChannels[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// SourceType
var globalSourceTypes;
function bindGlobalSourceTypes() {
    if (!globalSourceTypes) {
        $.getJSON('/admin/api/sourcetype/get').done(function (data) {
            globalSourceTypes = data;
        });
    }
}
function buildParamSourceTypes() {
    var param = '';
    if (globalSourceTypes != null) {
        for (var i = 0; i < globalSourceTypes.length; i++) {
            var id = globalSourceTypes[i].SourceTypeId;
            if (!$('#' + id + '_chkSourceType').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function sourceTypeFomatter(cellvalue, options, rowObject) {
    if (globalSourceTypes == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalSourceTypes, function (index) {

        if (globalSourceTypes[index].SourceTypeId == cellvalue) {
            retVal = globalSourceTypes[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}

// EducationLevel
var globalEducationLevels;
function bindGlobalEducationLevels() {
    if (!globalEducationLevels) {
        $.getJSON('/admin/api/educationlevel/get').done(function (data) {
            globalEducationLevels = data;
        });
    }
}
function buildParamEducationLevels() {
    var param = '';
    if (globalEducationLevels != null) {
        for (var i = 0; i < globalEducationLevels.length; i++) {
            var id = globalEducationLevels[i].EducationLevelId;
            if (!$('#' + id + '_chkEducationLevel').is(':checked')) continue;
            param += param.length == 0 ? id : ',' + id;
        }
    }
    return param;
}
function educationLevelFomatter(cellvalue, options, rowObject) {
    if (globalEducationLevels == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalEducationLevels, function (index) {

        if (globalEducationLevels[index].EducationLevelId == cellvalue) {
            //console.log(globalStatusMap[index].Name);
            retVal = globalEducationLevels[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}
function educationLevelUnformat(cellvalue, options) {
    //console.log(cellvalue);
    $.each(globalEducationLevels, function (index) {
        if (globalEducationLevels[index].Name == cellvalue) {
            return globalEducationLevels[index].EducationLevelId;
        }
    });
    return cellvalue;
}
function jqGridEducationLevelDropVal(elem, operation, value) {
    if (operation === 'get') {
        return $(elem).val();
    } else if (operation === 'set') {
        $(elem).val(value);
    }
};
function jqGridEducationLevelDropEl(value, options) {
    var retVal = $(document.createElement('select'));
    //retval.innerHTML = "<option value='1'>HN</option>";
    $(retVal).addClass("form-control");

    $.each(globalEducationLevels, function (index) {
        if (globalEducationLevels[index].EducationLevelId == value) {
            $(retVal).append("<option selected value='" + globalEducationLevels[index].EducationLevelId + "'>" + globalEducationLevels[index].Name + "</option>");
        } else {
            $(retVal).append("<option value='" + globalEducationLevels[index].EducationLevelId + "'>" + globalEducationLevels[index].Name + "</option>");
        }
    });
    //});
    return retVal;
}

// Lumped
var globalLumpeds;
function bindGlobalLumpeds() {
    if (!globalLumpeds) {
        $.getJSON('/admin/api/lumped/get').done(function (data) {
            globalLumpeds = data;
        });
    }
}
function lumpedFomatter(cellvalue, options, rowObject) {
    if (globalLumpeds == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalLumpeds, function (index) {

        if (globalLumpeds[index].Id == cellvalue) {
            retVal = globalLumpeds[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}
function jqGridLumpedDropVal(elem, operation, value) {
    if (operation === 'get') {
        return $(elem).val();
    } else if (operation === 'set') {
        $(elem).val(value);
    }
    return $(elem).val();
}
function jqGridLumpedDropEl(value, options) {
    var retVal = $(document.createElement('select'));
    $(retVal).addClass("form-control");

    $.each(globalLumpeds, function (index) {
        if (globalLumpeds[index].Id == value) {
            $(retVal).append("<option selected value='" + globalLumpeds[index].Id + "'>" + globalLumpeds[index].Name + "</option>");
        } else {
            $(retVal).append("<option value='" + globalLumpeds[index].Id + "'>" + globalLumpeds[index].Name + "</option>");
        }
    });
    //});
    return retVal;
}

// HaveJob
function haveJobFomatter(cellvalue, options, rowObject) {
    return cellvalue === undefined ||
        cellvalue == null ||
        cellvalue == 0 ||
        cellvalue == false ? 'Chưa' : 'Rồi';
}

// Enum UserLeader
var globalUserLeaders;
function userLeaderFomatter(cellvalue, options, rowObject) {
    if (globalUserLeaders == null) {
        return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
    }

    var retVal;
    $.each(globalUserLeaders, function (index) {

        if (globalUserLeaders[index].Id == cellvalue) {
            retVal = globalUserLeaders[index].Name;
        }
    });
    if (retVal) return retVal;
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? '' : cellvalue;
}
function jqGridUserLeaderDropVal(elem, operation, value) {
    if (operation === 'get') {
        return $(elem).val();
    } else if (operation === 'set') {
        $(elem).val(value);
    }
    return $(elem).val();
}
function jqGridUserLeaderDropEl(value, options) {
    var groupId = options.id.split('_')[0];
    var retVal = $(document.createElement('select'));
    $(retVal).addClass("form-control");

    $.each(globalUserLeaders, function (index) {
        {
            if (globalUserLeaders[index].Id == value) {
                $(retVal).append("<option selected value='" + globalUserLeaders[index].Id + "'>" + globalUserLeaders[index].Name + "</option>");
            } else {
                $(retVal).append("<option value='" + globalUserLeaders[index].Id + "'>" + globalUserLeaders[index].Name + "</option>");
            }
        }
    });
    //});
    return retVal;
}

function haveJobFormatter(cellvalue, options, rowObject) {
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? 'Chưa' : 'Rồi';
}

function haveLearnFormatter(cellvalue, options, rowObject) {
    return cellvalue === undefined || cellvalue == null || cellvalue == 0 ? 'Chưa' : 'Rồi';
}

function stringFormatter(cellvalue, options, rowObject) {
    return cellvalue === undefined || cellvalue == null ? '' : cellvalue;
}

function listenFomatter(cellvalue, options, rowObject) {
    if (cellvalue == null || cellvalue == '')
        return '';
    return '<audio controls style="width: 98%;">' +
        '<source src="' + cellvalue + '" type="audio/mpeg" />' +
        '</audio>';
}

// Checkbox
function checkboxFomatter(cellvalue, options, rowObject) {
    return "<input type='checkbox' name='checkbox'>";
}

function isCloseFomatter(cellvalue, options, rowObject) {
    return cellvalue == true ? "Đóng" : "Mở";
}

(function ($) {
    'use strict';
    /*jslint unparam: true */
    $.extend($.fn.fmatter, {
        dynamicLink: function (cellValue, options, rowData) {
            // href, target, rel, title, onclick
            // other attributes like media, hreflang, type are not supported currently
            var op = { url: '#' };

            if (typeof options.colModel.formatoptions !== 'undefined') {
                op = $.extend({}, op, options.colModel.formatoptions);
            }
            if ($.isFunction(op.target)) {
                op.target = op.target.call(this, cellValue, options.rowId, rowData, options);
            }
            if ($.isFunction(op.url)) {
                op.url = op.url.call(this, cellValue, options.rowId, rowData, options);
            }
            if ($.isFunction(op.cellValue)) {
                cellValue = op.cellValue.call(this, cellValue, options.rowId, rowData, options);
            }
            if ($.fmatter.isString(cellValue) || $.fmatter.isNumber(cellValue)) {
                return '<a' +
                    (op.target ? ' target=' + op.target : '') +
                    (op.onClick ? ' onclick="return $.fn.fmatter.dynamicLink.onClick.call(this, arguments[0]);"' : '') +
                    ' href="' + op.url + '">' +
                    (cellValue || '&nbsp;') + '</a>';
            } else {
                return '&nbsp;';
            }
        }
    });
    $.extend($.fn.fmatter.dynamicLink, {
        unformat: function (cellValue, options, elem) {
            var text = $(elem).text();
            return text === '&nbsp;' ? '' : text;
        },
        onClick: function (e) {
            var $cell = $(this).closest('td'),
                $row = $cell.closest('tr.jqgrow'),
                $grid = $row.closest('table.ui-jqgrid-btable'),
                p,
                colModel,
                iCol;

            if ($grid.length === 1) {
                p = $grid[0].p;
                if (p) {
                    iCol = $.jgrid.getCellIndex($cell[0]);
                    colModel = p.colModel;
                    colModel[iCol].formatoptions.onClick.call($grid[0],
                        $row.attr('id'), $row[0].rowIndex, iCol, $cell.text(), e);
                }
            }
            return false;
        }
    });
}(jQuery));

// Custom validate
function customValidate() {
    var elements = document.getElementsByTagName('INPUT');
    for (var i = 0; i < elements.length; i++) {
        elements[i].oninvalid = function (e) {
            e.target.setCustomValidity('');
            if (!e.target.validity.valid) {
                e.target.setCustomValidity(e.target.getAttribute('error'));
            }
        };
    }
}

function alertProcess() {
    $("#alert").html('');
    $("#alert").removeClass();
    $("#alert").addClass("alert alert-block alert-info");
    $("#alert").html("<i class='icon-spinner icon-spin orange bigger-125'></i>&nbsp;Đang xử lý...");
    document.body.scrollTop = document.documentElement.scrollTop = 0;
}

function alertError(message) {
    $("#alert").html('');
    $("#alert").removeClass();
    $("#alert").html(message);
    $("#alert").addClass("alert alert-block alert-warning");
    document.body.scrollTop = document.documentElement.scrollTop = 0;
};

function alertSuccess(message) {
    $("#alert").html('');
    $("#alert").removeClass();
    $("#alert").html(message);
    $("#alert").addClass("alert alert-block alert-success");
    document.body.scrollTop = document.documentElement.scrollTop = 0;
};

function alertHide(message) {
    $("#alert").html('');
    $("#alert").removeClass();
};

function checkAll(chkAll, chkItemName) {
    var elements = document.getElementsByName(chkItemName);
    for (var i = 0; i < elements.length; i++) {
        $(elements[i]).prop('checked', $('#' + chkAll).is(":checked"));
    }
}

function checkAllClassName(chkAll, chkClassName) {
    var elements = document.getElementsByClassName(chkClassName);
    for (var i = 0; i < elements.length; i++) {
        $(elements[i]).prop('checked', $('#' + chkAll).is(":checked"));
    }
}

function fixCheckBoxGrid(e) {
    e = e || event;/* get IE event ( not passed ) */
    e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
}

function loadStatusMap() {
    var statusCareId = $("#ddlStatusCare").val();
    var url = "/admin/api/statusmap/GetByStatusCareId?statusCareId=" + statusCareId;
    $.ajax({
        url: url,
        type: 'GET',
        success: function (data) {
            if (data == null) return;
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].Id + '">L' + data[i].LevelId + ' - ' + data[i].Name + '</option>';

            }
            $('#ddlStatusMap').empty().append(options);
        }
    });
}

function loadStatusMap2() {
    var statusCareId = $("#ddlStatusCare").val();
    var url = "/admin/api/statusmap/GetByStatusCareId?statusCareId=" + statusCareId;
    $.ajax({
        url: url,
        type: 'GET',
        success: function (data) {
            if (data == null) return;
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += data[i].LevelIdNext > 0 
                    ? '<option value="' + data[i].Id + '">' + data[i].Name + ' - L' + data[i].LevelIdNext + '</option>'
                    : '<option value="' + data[i].Id + '">' + data[i].Name + '</option>';

            }
            $('#ddlStatusMap').empty().append(options);
        }
    });
}

function loadUserByGroupId() {
    var groupId = $('#GroupId').val();
    if (groupId === undefined) return null;
    
    var url = "/admin/api/user/GetByGroupId?groupId=" + groupId;
    return $.ajax({
        url: url,
        type: 'GET',
        success: function (data) {
            if (data == null) return;
            
            var options = '';
            if (data.length > 1)
                options += '<option value="0">Tất cả</option>';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].UserID + '">' + data[i].FullName + '</option>';
            }
            $('#UserId').empty().append(options);
        }
    });
}

function GetConsultantNomrByBranchId() {
    var branchId = $('#BranchId').val();
    if (branchId === undefined) return null;

    var url = "/admin/api/user/GetConsultantNomrByBranchId?branchId=" + branchId;
    return $.ajax({
        url: url,
        type: 'GET',
        success: function (data) {
            if (data == null) return;

            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].UserID + '">' + data[i].FullName + '</option>';
            }
            $('#UserId').empty().append(options);
            $('#ConsultantId').empty().append(options);
        }
    });
}

function standardDatetime(datetime) {
    // DateTime
    var time;
    var value = Math.abs(new Date() - new Date(datetime));
    value = Math.round(value / 1000);
    if (value < 60)
        time = '<b>' + value + '</b>' + ' giây';
    else {
        value = Math.round(value / 60);
        if (value < 60)
            time = '<b>' + value + '</b>' + ' phút';
        else {
            value = Math.round(value / 60);
            if (value < 24)
                time = '<b>' + value + '</b>' + ' giờ';
            else {
                value = Math.round(value / 24);
                time = '<b>' + value + '</b>' + ' ngày';
            }
        }
    }
    return time + ' trước';
}

function validateEmail(value) {
    if (value == null || value.length == 0) return true;
    var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return value.match(regex);
}

function  validateMobile(value) {
    if (value == null || value.length == 0) return true;
    var regex = /^(?!0)((\d{3}-\d{3}-\d{4,5})|\d{9,10})$/;
    return value.match(regex);
}

function openDialog(id) {
    var params = [
        'height=' + screen.height,
        'width=' + screen.width,
        'fullscreen=yes',
        'scrollbars=yes'
    ].join(',');
    window.open("/admin/contactlevelinfo/edit/" + id, '', params);
}